const app = getApp();
const utils = require('../../../utils/utils.js');
Page({
  data: {
    picker: ['商户登录', '合作商登录'],
    index: 0,
    phone: "",
    getCode: "",
    lastTime: 0,
    code: ""
  },
  pickerChange(e) {
    // console.log(e);
    // 0 商户登录 1 合作商登录
    this.setData({
      index: e.detail.value
    })
  },
  // 输入手机号
  inputBlur(e){
    let phone = e.detail.value;
    this.setData({
      phone: phone
    });
  },
  // 获取短信验证码
  getDxCode:function() {
    let phone = this.data.phone;
    if (phone == "" || phone == null) {
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 2000
      });
      return;
    } else {
      let lastTime = this.data.lastTime;
      var timestamp = Date.parse(new Date());  
      timestamp = timestamp / 1000;
      if (timestamp - lastTime < 60) {
        wx.showToast({
          title: "您已获取验证码!",
          icon: 'none',
          duration: 2000
        });
        return;
      } else {
        let type = this.data.index;
        wx.showLoading({
          title: 'loading...',
          mask: true
        });
        // 0 商户登录 1 合作商登录
        if (type == 0) {
          type = 3;
        }
        let that = this;
        utils.AppHttpRequest('/common/getcode', {
          phone: phone,
          type: type
        }, function (res) {
            wx.hideLoading();
            console.log(res);
            if (res.code == 200) {
              // 获取验证码成功,将获取到验证码保存
              var timestamp = Date.parse(new Date());  
              timestamp = timestamp / 1000;
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
              });
              that.setData({
                getCode: res.data,
                lastTime: timestamp
              });
            }
        })
      }
    }
  },

  // 验证码绑定事件
  inputBlurCode:function(e) {
    let code = e.detail.value;
    this.setData({
      code: code
    });
  },
  // 进行登录操作
  submitTxLogin: function() {
    let phone = this.data.phone;
    if (phone == "" || phone == null) {
      wx.showToast({
        title: "请输入手机号!",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    let code = this.data.code;
    if (code == "" || code == null) {
      wx.showToast({
        title: "请输入短信验证码!",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    let type = this.data.index;
    if (type == 0) {
      type = 3;
    }
    let that = this;
    utils.AppHttpRequest('/common/login', {
      phone: phone,
      type: type,
      logintype: 1,
      pwd: code,
      logintFs: "minApp"
    }, function (res) {
        wx.hideLoading();
        console.log("login-->" + res);
        if (res.code == 200) {
          app.globalData.proxuUser = res.data;
          app.globalData.loginType = type;
          wx.reLaunch({
            url: '../../tx/index/index?data=' + res.data
          });
        }
    })
  },
  onHide: function () {
    this.setData({
      index: 0,
      phone: "",
      getCode: "",
      lastTime: 0,
      code: ""
    })
  },
  onUnload: function () {
      this.setData({
        index: 0,
        phone: "",
        getCode: "",
        lastTime: 0,
        code: ""
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})